<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_ipdata_convert($ip) {
	$return = '';
	
	if( preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip) ) {
		$ips = explode('.', $ip);
		
		if( $ips['0'] == 10 || $ips['0'] == 127 || ($ips['0'] == 192 && $ips['1'] == 168) || ($ips['0'] == 172 && $ips['1'] >= 16 && $ips['1'] <= 31) ) {
			$return = '- LAN';
		} elseif( $ips['0'] == 255 || $ips['0'] == 255 || $ips['0'] == 255 || $ips['0'] == 255 ) {
			$return = '- Invalid IP Address';
		} else {
			$ipdatafilea = CONFIG_PATH . CONFIG_DIR_METADATA . 'ipdata/tinyipdata.dat';
			$ipdatafileb = CONFIG_PATH . CONFIG_DIR_METADATA . 'ipdata/wry.dat';
			if( @file_exists($ipdatafilea) ) {
				$return = func_ipdata_convertA($ip, $ipdatafilea);
			} else {
				$return = func_ipdata_convertB($ip, $ipdatafileb);
			}
		}
	}
	
	return $return;
}

//
function func_ipdata_convert_detail($ip) {
	$state = '';
	$stateb = '';
	$city = '';
	$cityb = '';
	$vendor = '';
	
	$data = func_ipdata_convert($ip);
	if( $data == '- LAN' || $data == '- Invalid IP Address' || $data == '- Invalid IP data file' || $data == '- Unknown' ) {
		//
	} else {
		$a = explode('-', $data);
		if( isset($a['1']) && strlen($a['1']) > 0 ) {
			if( ($ps = strpos($a['1'], "省")) !== false ) {
				$state = trim(substr($a['1'], 0, $ps));
				$stateb = trim(substr($a['1'], 0, $ps+3));
			}
			if( $ps > 0 && ($pc = strpos($a['1'], "市")) !== false ) {
				$city = trim(substr($a['1'], $ps+3, $pc-$ps-3));
				$cityb = trim(substr($a['1'], $ps+3, $pc-$ps));
			}
		}
		if( isset($a['2']) && strlen($a['2']) > 0 ) {
			$vendor = trim($a['2']);
		}
	}
	
	$detail = array(
			'data' => $str,
			'state' => $state,
			'stateb' => $stateb,
			'city' => $city,
			'cityb' => $cityb,
			'vendor' => $vendor,
				);
	
	return $detail;
}

//
function func_ipdata_convertA($ip, $ipdatafile) {
	static $fp = NULL, $offset = array(), $index = NULL;

	$ipdot = explode('.', $ip);
	$ip    = pack('N', ip2long($ip));

	$ipdot[0] = (int)$ipdot[0];
	$ipdot[1] = (int)$ipdot[1];

	if( $fp === NULL && $fp = @fopen($ipdatafile, 'rb') ) {
		$offset = unpack('Nlen', fread($fp, 4));
		$index  = fread($fp, $offset['len'] - 4);
	} elseif( $fp == FALSE) {
		return  '- Invalid IP data file';
	}

	$length = $offset['len'] - 1028;
	$start  = unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
	for($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {
		if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
			$index_offset = unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
			$index_length = unpack('Clen', $index{$start + 7});
			break;
		}
	}
	fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
	if( $index_length['len'] ) {
		return '- ' . fread($fp, $index_length['len']);
	} else {
		return '- Unknown';
	}
}

//
function func_ipdata_convertB($ip, $ipdatafile) {

}
//
?>